package com.wjw.service;

import com.wjw.mapper.RoadBasicTableMapper;
import com.wjw.model.Road_basic_table;
import com.wjw.model.Road_type_table;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Road_basic_table_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoadBasicTableService {


//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private RoadBasicTableMapper mapper=sqlsession.getMapper(RoadBasicTableMapper.class);


    public List<Road_basic_table_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadBasicTableMapper mapper=sqlsession.getMapper(RoadBasicTableMapper.class);
        try {
            List<Road_basic_table> all = mapper.findAll();
            List<Road_basic_table_view> list= new ArrayList<Road_basic_table_view>();
            for(Road_basic_table e:all){
                Road_basic_table_view k = new Road_basic_table_view();
                k.setRoad_basic_AADT(e.getRoad_basic_AADT());
                k.setRoad_basic_AADT_grade(e.getRoad_basic_AADT_grade());
                k.setRoad_basic_area(e.getRoad_basic_area());
                k.setRoad_basic_build_date(e.getRoad_basic_build_date());
                k.setRoad_basic_condition(e.getRoad_basic_condition());
                k.setRoad_basic_design(e.getRoad_basic_design());
                k.setRoad_basic_end(e.getRoad_basic_end());
                k.setRoad_basic_id(e.getRoad_basic_id());
                k.setRoad_basic_length(e.getRoad_basic_length());
                k.setRoad_basic_lister(e.getRoad_basic_lister());
                k.setRoad_basic_look_unit(e.getRoad_basic_look_unit());
                k.setRoad_basic_made(e.getRoad_basic_made());
                k.setRoad_basic_manager_type(e.getRoad_basic_manager_type());
                k.setRoad_basic_manager_unit(e.getRoad_basic_manager_unit());
                k.setRoad_basic_name(e.getRoad_basic_name());
                k.setRoad_basic_reviewer(e.getRoad_basic_reviewer());
                k.setRoad_basic_speed(e.getRoad_basic_speed());
                k.setRoad_basic_start(e.getRoad_basic_start());
                k.setRoad_basic_table_date(e.getRoad_basic_table_date());
                k.setRoad_basic_table_unit(e.getRoad_basic_table_unit());
                k.setRoad_basic_town(e.getRoad_basic_town());
                k.setRoad_basic_trend(e.getRoad_basic_trend());
                k.setRoad_basic_width(e.getRoad_basic_width());
                k.setRoad_type_name(mapper.findRoadTypeById(e.getRoad_type_id()).getRoad_type_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Road_basic_table_view> findByPage(int page,int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadBasicTableMapper mapper=sqlsession.getMapper(RoadBasicTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Road_basic_table> all = mapper.findByPage(start,limit);
            List<Road_basic_table_view> list= new ArrayList<Road_basic_table_view>();
            for(Road_basic_table e:all){
                Road_basic_table_view k = new Road_basic_table_view();
                k.setRoad_basic_AADT(e.getRoad_basic_AADT());
                k.setRoad_basic_AADT_grade(e.getRoad_basic_AADT_grade());
                k.setRoad_basic_area(e.getRoad_basic_area());
                k.setRoad_basic_build_date(e.getRoad_basic_build_date());
                k.setRoad_basic_condition(e.getRoad_basic_condition());
                k.setRoad_basic_design(e.getRoad_basic_design());
                k.setRoad_basic_end(e.getRoad_basic_end());
                k.setRoad_basic_id(e.getRoad_basic_id());
                k.setRoad_basic_length(e.getRoad_basic_length());
                k.setRoad_basic_lister(e.getRoad_basic_lister());
                k.setRoad_basic_look_unit(e.getRoad_basic_look_unit());
                k.setRoad_basic_made(e.getRoad_basic_made());
                k.setRoad_basic_manager_type(e.getRoad_basic_manager_type());
                k.setRoad_basic_manager_unit(e.getRoad_basic_manager_unit());
                k.setRoad_basic_name(e.getRoad_basic_name());
                k.setRoad_basic_reviewer(e.getRoad_basic_reviewer());
                k.setRoad_basic_speed(e.getRoad_basic_speed());
                k.setRoad_basic_start(e.getRoad_basic_start());
                k.setRoad_basic_table_date(e.getRoad_basic_table_date());
                k.setRoad_basic_table_unit(e.getRoad_basic_table_unit());
                k.setRoad_basic_town(e.getRoad_basic_town());
                k.setRoad_basic_trend(e.getRoad_basic_trend());
                k.setRoad_basic_width(e.getRoad_basic_width());
                k.setRoad_type_name(mapper.findRoadTypeById(e.getRoad_type_id()).getRoad_type_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Road_basic_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadBasicTableMapper mapper=sqlsession.getMapper(RoadBasicTableMapper.class);
        try {
            Road_basic_table k = new Road_basic_table();
            k.setRoad_basic_AADT(e.getRoad_basic_AADT());
            k.setRoad_basic_AADT_grade(e.getRoad_basic_AADT_grade());
            k.setRoad_basic_area(e.getRoad_basic_area());
            k.setRoad_basic_build_date(e.getRoad_basic_build_date());
            k.setRoad_basic_condition(e.getRoad_basic_condition());
            k.setRoad_basic_design(e.getRoad_basic_design());
            k.setRoad_basic_end(e.getRoad_basic_end());
            k.setRoad_basic_id(e.getRoad_basic_id());
            k.setRoad_basic_length(e.getRoad_basic_length());
            k.setRoad_basic_lister(e.getRoad_basic_lister());
            k.setRoad_basic_look_unit(e.getRoad_basic_look_unit());
            k.setRoad_basic_made(e.getRoad_basic_made());
            k.setRoad_basic_manager_type(e.getRoad_basic_manager_type());
            k.setRoad_basic_manager_unit(e.getRoad_basic_manager_unit());
            k.setRoad_basic_name(e.getRoad_basic_name());
            k.setRoad_basic_reviewer(e.getRoad_basic_reviewer());
            k.setRoad_basic_speed(e.getRoad_basic_speed());
            k.setRoad_basic_start(e.getRoad_basic_start());
            k.setRoad_basic_table_date(e.getRoad_basic_table_date());
            k.setRoad_basic_table_unit(e.getRoad_basic_table_unit());
            k.setRoad_basic_town(e.getRoad_basic_town());
            k.setRoad_basic_trend(e.getRoad_basic_trend());
            k.setRoad_basic_width(e.getRoad_basic_width());
            k.setRoad_type_id(mapper.findRoadTypeByName(e.getRoad_type_name()).getRoad_type_id());
            mapper.add(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadBasicTableMapper mapper=sqlsession.getMapper(RoadBasicTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Road_basic_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        RoadBasicTableMapper mapper=sqlsession.getMapper(RoadBasicTableMapper.class);
        try {
            Road_basic_table k = new Road_basic_table();
            k.setRoad_basic_AADT(e.getRoad_basic_AADT());
            k.setRoad_basic_AADT_grade(e.getRoad_basic_AADT_grade());
            k.setRoad_basic_area(e.getRoad_basic_area());
            k.setRoad_basic_build_date(e.getRoad_basic_build_date());
            k.setRoad_basic_condition(e.getRoad_basic_condition());
            k.setRoad_basic_design(e.getRoad_basic_design());
            k.setRoad_basic_end(e.getRoad_basic_end());
            k.setRoad_basic_id(e.getRoad_basic_id());
            k.setRoad_basic_length(e.getRoad_basic_length());
            k.setRoad_basic_lister(e.getRoad_basic_lister());
            k.setRoad_basic_look_unit(e.getRoad_basic_look_unit());
            k.setRoad_basic_made(e.getRoad_basic_made());
            k.setRoad_basic_manager_type(e.getRoad_basic_manager_type());
            k.setRoad_basic_manager_unit(e.getRoad_basic_manager_unit());
            k.setRoad_basic_name(e.getRoad_basic_name());
            k.setRoad_basic_reviewer(e.getRoad_basic_reviewer());
            k.setRoad_basic_speed(e.getRoad_basic_speed());
            k.setRoad_basic_start(e.getRoad_basic_start());
            k.setRoad_basic_table_date(e.getRoad_basic_table_date());
            k.setRoad_basic_table_unit(e.getRoad_basic_table_unit());
            k.setRoad_basic_town(e.getRoad_basic_town());
            k.setRoad_basic_trend(e.getRoad_basic_trend());
            k.setRoad_basic_width(e.getRoad_basic_width());
            k.setRoad_type_id(mapper.findRoadTypeByName(e.getRoad_type_name()).getRoad_type_id());
            mapper.change(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }
}
